//
//  CalendarEventCell.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/9/22.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "CalendarEventCell.h"
#import "CalendarEvent.h"
#import "CoreDataHelper.h"
#import "CalendarCollectionViewEditableLayout.h"

// bodyView左右22.f各有透明leftDrag/rightDrag
CGFloat const CalendarCellPadding = 22.0f;
NSTimeInterval const DefaultCellDuration = 2 * 60 * 60;  //seconds 默认2小时

@interface CalendarEventCell ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView    *borderView;  // 最侧窄边

@property (nonatomic, strong) UIView    *leftCircleView;
@property (nonatomic, strong) UIView    *rightCircleView;

@property (nonatomic) UIEdgeInsets   cellMargin;

@end

@implementation CalendarEventCell

-(instancetype)init {
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (frame.size.width == 0) {
        frame = CGRectMake(0, 0, 4 * CalendarCellPadding, 44);
    }
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)setSelected:(BOOL)selected
{
    
    if (selected && (self.selected != selected)) {
        self.bodyView.layer.shadowOpacity = 0.2;
    } else if (selected) {
        self.bodyView.layer.shadowOpacity = 0.2;
    } else {
        self.bodyView.layer.shadowOpacity = 0.0;
    }
    
    [super setSelected:selected];    // Must be here for animation to fire
    [self updateColors];
    [self circleHidden:!selected];
}

#pragma mark - EventCell
@synthesize event = _event;
- (void)setEvent:(CalendarEvent *)event
{
    _event = event;
    if (event.title != nil) {
        self.title.attributedText = [[NSAttributedString alloc] initWithString:event.title attributes:[self titleAttributesHighlighted:self.selected]];
        self.title.adjustsFontSizeToFitWidth = YES;
    }
    if (event.location != nil) {
        self.location.attributedText = [[NSAttributedString alloc] initWithString:event.location attributes:[self subtitleAttributesHighlighted:self.selected]];
        self.location.adjustsFontSizeToFitWidth = YES;
    }
    if (event.subject != nil) {
        self.subject.attributedText = [[NSAttributedString alloc] initWithString:event.subject attributes:[self subtitleAttributesHighlighted:self.selected]];
        self.subject.adjustsFontSizeToFitWidth = YES;
    }
}

- (void)updateColors
{
    self.bodyView.backgroundColor = [self backgroundColorHighlighted:self.selected];
    self.borderView.backgroundColor = [self borderColor];
    self.title.textColor = [self textColorHighlighted:self.selected];
    self.location.textColor = [self textColorHighlighted:self.selected];
}

- (NSDictionary *)titleAttributesHighlighted:(BOOL)highlighted
{
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.hyphenationFactor = 1.0;
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    return @{
        NSFontAttributeName : [UIFont boldSystemFontOfSize:10.0],
        NSForegroundColorAttributeName : [self textColorHighlighted:highlighted],
        NSParagraphStyleAttributeName : paragraphStyle
    };
}

- (NSDictionary *)subtitleAttributesHighlighted:(BOOL)highlighted
{
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.hyphenationFactor = 1.0;
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    return @{
        NSFontAttributeName : [UIFont systemFontOfSize:10.0],
        NSForegroundColorAttributeName : [self textColorHighlighted:highlighted],
        NSParagraphStyleAttributeName : paragraphStyle
    };
}

- (UIColor *)backgroundColorHighlighted:(BOOL)selected
{
    return selected ? [UIColor colorWithHexString:@"35b1f1"] : [[UIColor colorWithHexString:@"35b1f1"] colorWithAlphaComponent:0.2];
}

- (UIColor *)textColorHighlighted:(BOOL)selected
{
    return selected ? [UIColor whiteColor] : [UIColor colorWithHexString:@"21729c"];
}

- (UIColor *)borderColor
{
    return [[self backgroundColorHighlighted:NO] colorWithAlphaComponent:1.0];
}

//隐藏cell但是能够响应手势事件
-(void)cellHidden:(BOOL)hidden{
    if (hidden == YES) {
        UIColor *clearColor = [UIColor clearColor];
        self.bodyView.backgroundColor = clearColor;
        [self circleHidden:YES];
        self.borderView.backgroundColor = clearColor;
        self.title.textColor = clearColor;
        self.location.textColor = clearColor;
    }
    else{
        [self circleHidden:NO];
        [self updateColors];
    }
}

-(void)circleHidden:(BOOL)hidden{
    [self.leftCircleView setHidden:hidden];
    [self.rightCircleView setHidden:hidden];
}

// 画小圆圈
-(UIView *)drawCircle{
    UIView *circleView = [[UIView alloc] init];
    //创建出CAShapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = circleView.frame;
    shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    //设置线条的宽度和颜色
    shapeLayer.lineWidth = 1.2f;
    shapeLayer.strokeColor = [UIColor colorWithHexString:@"35b1f1"].CGColor;
    //创建出圆形贝塞尔曲线
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 7, 7)];
    shapeLayer.path = circlePath.CGPath;
    [circleView.layer addSublayer:shapeLayer];
    
    return circleView;
}

-(void)initialize{
    self.defaultCenterY = self.center.y;
    self.cellMargin = UIEdgeInsetsMake(0, CalendarCellPadding, 0, CalendarCellPadding);
    
    self.backgroundColor = [UIColor clearColor];
    
    self.leftCircleView = [[UIView alloc] init];
    self.rightCircleView = [[UIView alloc] init];
    // body
    self.bodyView = [[UIView alloc] init];
    self.bodyView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    self.bodyView.layer.shouldRasterize = YES;
    
    self.bodyView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.bodyView.layer.shadowOffset = CGSizeMake(0.0, 4.0);
    self.bodyView.layer.shadowRadius = 5.0;
    self.bodyView.layer.shadowOpacity = 0.0;   //透明
    [self addSubview:self.bodyView];
    
    self.borderView = [UIView new];
    [self addSubview:self.borderView];
    
    self.title = [UILabel new];
    self.title.numberOfLines = 0;
    self.title.backgroundColor = [UIColor clearColor];
    [self addSubview:self.title];
    
    self.subject = [UILabel new];
    self.subject.numberOfLines = 0;
    self.subject.backgroundColor = [UIColor clearColor];
    [self addSubview:self.subject];
    
    self.location = [UILabel new];
    self.location.numberOfLines = 0;
    self.location.backgroundColor = [UIColor clearColor];
    [self addSubview:self.location];
    
    // left
    self.leftDrag = [[UIControl alloc] init];
    self.leftDrag.backgroundColor = [UIColor clearColor];
    [self addSubview:self.leftDrag];
    
    // right
    self.rightDrag = [[UIControl alloc] init];
    self.rightDrag.backgroundColor = [UIColor clearColor];
    [self addSubview:self.rightDrag];
    
    self.leftCircleView = [self drawCircle];
    self.rightCircleView = [self drawCircle];
    [self.leftDrag addSubview:self.leftCircleView];
    [self.rightDrag addSubview:self.rightCircleView];
    [self circleHidden:YES];
    
    // auto layout
    [self.leftDrag makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.left);
        make.centerY.equalTo(self.centerY);
        make.height.equalTo(self.height);
        make.width.equalTo(@(self.cellMargin.left * 2));
    }];
    
    [self.rightDrag makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.right);
        make.centerY.equalTo(self.centerY);
        make.height.equalTo(self.leftDrag.height);
        make.width.equalTo(self.leftDrag.width);
    }];
    
    [self.bodyView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftDrag.centerX);
        make.right.equalTo(self.rightDrag.centerX);
        make.top.equalTo(self.top);
        make.bottom.equalTo(self.bottom);
    }];
    
    [self.leftCircleView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.leftDrag.centerX);
        make.centerY.equalTo(self.leftDrag.centerY);
        make.width.equalTo(@7);
        make.height.equalTo(@7);
    }];
    
    [self.rightCircleView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.rightDrag.centerX);
        make.centerY.equalTo(self.rightDrag.centerY);
        make.width.equalTo(self.leftCircleView.width);
        make.height.equalTo(self.leftCircleView.height);
    }];
    
    CGFloat borderWidth = 2.0;
    CGFloat contentMargin = 2.0;
    UIEdgeInsets contentPadding = UIEdgeInsetsMake(6.0, (borderWidth + 6.0), 6.0, 6.0);
    
    [self.borderView makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.height);
        make.width.equalTo(@(borderWidth));
        make.left.equalTo(self.bodyView.left);
        make.top.equalTo(self.top);
    }];
    
    [self.title makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bodyView.top).offset(contentPadding.top);
        make.left.equalTo(self.bodyView.left).offset(contentPadding.left);
        make.right.equalTo(self.bodyView.right).offset(-contentPadding.right);
    }];
    
    [self.subject makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.title.bottom).offset(contentMargin);
        make.left.equalTo(self.bodyView.left).offset(contentPadding.left);
        make.right.equalTo(self.bodyView.right).offset(-contentPadding.right);
        make.bottom.lessThanOrEqualTo(self.bodyView.bottom).offset(-contentPadding.bottom);
    }];
    
    [self updateColors];
    
}

@end
